{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '1.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 1. 打开文件\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# f = open(\"d:\\\\1.txt\",'r')\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m# 2. 读取内容(三种方式)\u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 2.1 读取所有\u001b[39;00m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# str = f.read()\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m# print(line)\u001b[39;00m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;66;03m# 2.3 读取所有行\u001b[39;00m\n\u001b[0;32m     12\u001b[0m lines \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mreadlines()\n",
      "File \u001b[1;32mc:\\Users\\TEA-301\\anaconda3\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    318\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    319\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    320\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    321\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    322\u001b[0m     )\n\u001b[1;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '1.txt'"
     ]
    }
   ],
   "source": [
    "# 1. 打开文件\n",
    "# f = open(\"d:\\\\1.txt\",'r')\n",
    "f = open(\"1.txt\",'r')\n",
    "# 2. 读取内容(三种方式)\n",
    "# 2.1 读取所有\n",
    "# str = f.read()\n",
    "# print(str)\n",
    "# 2.2 读取一行\n",
    "# line = f.readline()\n",
    "# print(line)\n",
    "# 2.3 读取所有行\n",
    "lines = f.readlines()\n",
    "print(lines)\n",
    "# 3. 关闭\n",
    "f.close()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于路径\n",
    "# 1. 绝对路径  c:\\\\   d:\\\\ 开头的路径\n",
    "# d:\\\\1.txt \n",
    "# d:\\\\abc\\\\1.txt\n",
    "# 2. 相对路径(相对于程序运行路径下的相对位置)\n",
    "# 1.txt     \n",
    "# ./1.txt   相对路径(当前路径下的1.txt)\n",
    "# ../1.txt  上一层目录下的1.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aa\n",
      "\n",
      "\n",
      "\n",
      "asdfasdfasdfasd\n",
      "\n",
      "fa\n",
      "\n",
      "sdf\n",
      "\n",
      "asdfasdfasdffa\n",
      "\n",
      "sdfasd\n",
      "\n",
      "f\n"
     ]
    }
   ],
   "source": [
    "# 1. 打开文件\n",
    "f = open('2.txt','r')\n",
    "# 2. 一行行读取\n",
    "for line in f:\n",
    "    print(line)\n",
    "# 3. 关闭文件\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写入文件\n",
    "f = open(\"3.txt\",\"w\")\n",
    "f.write(\"你好!\\n python\\n 123\")\n",
    "f.close()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"3.txt\",\"a\")\n",
    "f.write(\"你好!\\n python\\n 123\")\n",
    "f.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写入文件\n",
    "f = open(\"4.txt\",\"w\")\n",
    "f.writelines(['a\\n','abcd\\n','def\\n'])\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "静夜思\n",
      "床前明月光，\n",
      "疑是地上霜。\n",
      "举头望明月，\n",
      "低头思故乡。\n",
      "\n",
      "\n",
      "\n",
      "疑是地上霜。\n",
      "举头望明月，\n",
      "低头思故乡。\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 文件的指针\n",
    "\n",
    "f = open(\"1.txt\",\"r\")\n",
    "str = f.read()\n",
    "print(str)\n",
    "# 字节为单位  一个汉字要3个字节  一个换行符1个字节\n",
    "f.seek(29,0)\n",
    "str = f.read()\n",
    "print(str)\n",
    "f.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaaaaaaaaASDFASDFASDFASDFA\n",
      "bbbbbbbb\n",
      "\n",
      "SDFASDFASDA tEST\n",
      "ASDFASDFASDFASD\n",
      "ASDFASDFASDFASDSDFASDF\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 打开文件 ( 读写方式)\n",
    "f = open(\"1.txt\",'r+')\n",
    "s = f.read()\n",
    "print(s)\n",
    "s = s.swapcase()\n",
    "# 回到文件的开头\n",
    "f.seek(0,0) \n",
    "f.write(s)\n",
    "# 关闭文件\n",
    "f.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '张三', '18\\n']\n",
      "['2', '李四', '19']\n"
     ]
    }
   ],
   "source": [
    "f = open(\"stus.csv\",'r')\n",
    "for line in f:\n",
    "    # print(line)\n",
    "    info = line.split(\",\")\n",
    "    print(info)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
